﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Collections;

namespace TCPServer
{
    public partial class Form1 : Form
    {
        private String STRING_ONLINE = "Service is now online.";
        private String STRING_OFFLINE = "Service is now offline.";
        private String STRING_HANDUP_MESSAGE = "Server handup.";
        private String STRING_SERVER_SAYS = "Server says: ";
        private Communication _socketCommunication;

        private delegate void ChangeInvoke(string str);
        
        public Form1()
        {
            InitializeComponent();            
        }

        public Form1(Communication socketCommunication)
        {
            InitializeComponent();
            _socketCommunication = socketCommunication;
            _socketCommunication.UserListChange += UserListEventHandler;
            _socketCommunication.MessgaeListChange += MessgaeListEventHandler;
            _socketCommunication.ClearListChange += ListClearEventHandler;
        }

        private void startService_Click(object sender, EventArgs e)
        {
            _socketCommunication.IntializeService(serverPortTextBox.Text.Trim());
            
            try
            {
                _socketCommunication.StartTread();    
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            statuBar.Text = STRING_ONLINE;
            ButtonEnableChange();
        }

        private void send_Click(object sender, EventArgs e)
        {
            String brocastMessage = STRING_SERVER_SAYS + sendMessageTextBox.Text;
            _socketCommunication.BrocastMessage(brocastMessage);
            sendMessageTextBox.Text = "";
        }

        private void stopServiceButton_Click(object sender, EventArgs e)
        {
            _socketCommunication.BrocastMessage(STRING_HANDUP_MESSAGE);
            _socketCommunication.Offline();
            statuBar.Text = STRING_OFFLINE;
            ButtonEnableChange();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            _socketCommunication.Offline();
        }

        private void ButtonEnableChange()
        {
            sendButton.Enabled = _socketCommunication.IsListening;
            startServiceButton.Enabled = !_socketCommunication.IsListening;
            stopServiceButton.Enabled = _socketCommunication.IsListening;
        }

        private void UserListEventHandler(string changeClient, bool changeAD)
        {
            lock (this.userListBox)
            {
                if (changeAD)
                {
                    UserListBoxInvokeAdd(changeClient);
                }
                else
                {
                    UserListBoxInvokeRemove(changeClient);
                }
            }
        }

        private void UserListBoxInvokeAdd(String msg)
        { 
            if (userListBox.InvokeRequired)
            {
                ChangeInvoke _myinvoke = new ChangeInvoke(UserListBoxInvokeAdd);
                userListBox.Invoke(_myinvoke, new object[] { msg});
            }
            else
            {
                this.userListBox.Items.Add(msg);
            }
        }

        private void UserListBoxInvokeRemove(String msg)
        {
            if (userListBox.InvokeRequired)
            {
                ChangeInvoke _myinvoke = new ChangeInvoke(UserListBoxInvokeRemove);
                userListBox.Invoke(_myinvoke, new object[] { msg });
            }
            else
            {
                this.userListBox.Items.Remove(msg);
            }
        }

        private void MessgaeListEventHandler(string msg)
        {
            MessgaeListInvoke(msg + "\r\n");
        }

        public void MessgaeListInvoke(string msg)
        {
            if (showinfoRichTextBox.InvokeRequired)
            {
                ChangeInvoke _myinvoke = new ChangeInvoke(MessgaeListInvoke);
                showinfoRichTextBox.Invoke(_myinvoke, new object[] { msg });
            }
            else
            {
                showinfoRichTextBox.AppendText(msg);
                showinfoRichTextBox.ScrollToCaret();
                showinfoRichTextBox.Refresh();
            }
        }

        private void ListClearEventHandler()
        {
            userListBox.Items.Clear();
            showinfoRichTextBox.Clear();
        }
    }       
}
